package com.example.controller;


import com.example.bean.ApiResult;
import com.example.bean.SuperAdmin;
import com.example.service.SuperAdminService;
import com.example.utils.ApiResultHandler;
import com.example.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/superAdmin")
@CrossOrigin
public class SuperAdminController {

    @Autowired
    private SuperAdminService superAdminService;

    //查看超级管理员个人信息
    @GetMapping("/selectById/{superAdmId}")
    public ApiResult selectSuperAdmById(@PathVariable String superAdmId){
        SuperAdmin res = superAdminService.selectSuperAdmById(superAdmId);
        if(res!=null)  return ApiResultHandler.buildApiResult(200,"根据超级管理员id查询",res);
        return ApiResultHandler.buildApiResult(400,"无此超级管理员信息",res);
    }

    //修改超级管理员个人信息
    @PutMapping("/update")
    public ApiResult updateSuperAdm(@RequestBody SuperAdmin superAdmin){
        String password = superAdmin.getSuperAdmPassword();
        password = Md5.md5(password);   //加密
        superAdmin.setSuperAdmPassword(password);
        int res = superAdminService.updateSuperAdm(superAdmin);
        if(res!=0)  return ApiResultHandler.buildApiResult(200,"更新超级管理员信息成功",res);
        return ApiResultHandler.buildApiResult(400,"更新超级管理员信息失败",res);
    }

    //添加超级管理员
    @PostMapping("/insert")
    public ApiResult insertSuperAdm(@RequestBody SuperAdmin superAdmin){
        String password = superAdmin.getSuperAdmPassword();
        password = Md5.md5(password);   //加密
        superAdmin.setSuperAdmPassword(password);
        int res = superAdminService.insertSuperAdm(superAdmin);
        if(res!=0)  return ApiResultHandler.buildApiResult(200,"添加超级管理员成功",res);
        return ApiResultHandler.buildApiResult(400,"添加超级管理员失败",res);
    }

    //删除超级管理员
    @DeleteMapping("/delete/{superAdmId}")
    public ApiResult deleteSuperAdm(@PathVariable String superAdmId){
        int res = superAdminService.deleteSuperAdm(superAdmId);
        if(res!=0)  return ApiResultHandler.buildApiResult(200,"删除超级管理员信息成功",res);
        return ApiResultHandler.buildApiResult(400,"删除超级管理员信息失败",res);
    }


}
